package com.ohimy.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.ohimy.dao.impl.AdminDao;
import com.ohimy.dao.impl.UserDao;
import com.ohimy.dao.impl.UserDetailDao;
import com.ohimy.dao.impl.UserFriendShipDao;
import com.ohimy.entity.Admin;
import com.ohimy.entity.User;
import com.ohimy.entity.UserDetail;
import com.ohimy.entity.UserFriendShip;
import com.ohimy.util.AdminLogin;

@Service("userService")
public class UserService {

	@Resource(name = "userDao")
	private UserDao userDao;

	@Resource(name = "adminDao")
	private AdminDao adminDao;

	@Resource(name = "userDetailDao")
	private UserDetailDao userDetailDao;

	@Resource(name = "userFriendShipDao")
	private UserFriendShipDao userFriendShipDao;

	/*
	 * 创建一个会员登录信息
	 * 
	 * @see com.ohimy.service.IUserService#createUser(com.ohimy.entity.User)
	 */
	public void createUser(User user) {
		userDao.save(user);
		userDetailDao.save(user.getUserDetail());
	}

	/*
	 * 创建一个会员基本信息
	 * 
	 * @see com.ohimy.service.IUserService#createUser(com.ohimy.entity.User)
	 */
	public void createUserDetail(UserDetail userDetail) {
		userDetailDao.save(userDetail);
	}

	/*
	 * 更新会员信息
	 * 
	 * @see com.ohimy.service.IUserService#updateUser(com.ohimy.entity.User)
	 */
	public void updateUser(User user) {
		userDao.update(user);
		userDetailDao.update(user.getUserDetail());
	}

	/*
	 * 更新会员信息
	 * 
	 * @see com.ohimy.service.IUserService#updateUser(com.ohimy.entity.User)
	 */
	public void updateUserDetail(UserDetail userDetail) {
		userDetailDao.merge(userDetail);
	}

	/*
	 * 删除会员
	 * 
	 * @see com.ohimy.service.IUserService#deleteUser(com.ohimy.entity.User)
	 */
	public void deleteUser(long id) {
		userDao.delete(userDao.get(id));
	}

	/**
	 * 判断是否为注册会员
	 * 
	 */
	public boolean isUser(User user) {
		Criterion[] criterions = new Criterion[] {
				Restrictions.eq("email", user.getEmail()),
				Restrictions.eq("password", user.getPassword()) };
		if (userDao.findByCriteria(criterions) != null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断是否为管理员
	 * 
	 * @see com.ohimy.service.IUserService#isAdmin(com.ohimy.entity.User)
	 */
	public boolean isAdmin(Admin admin) {
		Criterion[] criterions = new Criterion[] {
				Restrictions.eq("adminname", admin.getAdminname()),
				Restrictions.eq("adminpassword", admin.getAdminpassword()) };
		AdminLogin adminLogin = new AdminLogin();
		if (adminDao.findByCriteria(criterions) != null
				&& adminLogin.isAdmin(admin.getSecurityCode())) {
			return true;
		} else {
			return false;
		}
	}

	/*
	 * 插入浏览时间
	 * 
	 * @see com.ohimy.service.IUserService#insertVisitTime(com.ohimy.entity.User)
	 */
	public void insertVisitTime(User user) {
		user = findUserIdByEntity(user);
		user.setLast_visit(new Date());
		userDao.update(user);
	}

	/**
	 * 得到该会员的朋友数量
	 * 
	 * @param uid
	 * @return
	 */
	public long getFriendNumber(long uid) {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(UserFriendShip.class);
		criteria.add(Restrictions.eq("userId", uid));
		return userFriendShipDao.findByCriteria(criteria).size();
	}

	/*
	 * 通过id查找会员
	 * 
	 * @see com.ohimy.service.IUserService#findUserById(int)
	 */
	public User findUserById(long id) {
		return userDao.get(id);
	}

	public UserDetail findUserDetailById(long id) {
		return userDetailDao.get(id);
	}

	public User findUserIdByEntity(User user) {
		Criterion[] criterions = new Criterion[] { Restrictions.eq("email",
				user.getEmail()) };
		return userDao.findByCriteria(criterions).get(0);
	}

	public Admin findAdminIdByEntity(Admin admin) {
		Criterion[] criterions = new Criterion[] { Restrictions.eq("adminname",
				admin.getAdminname()) };
		return adminDao.findByCriteria(criterions).get(0);
	}

	/*
	 * 列出所有会员信息
	 * 
	 * @see com.ohimy.service.IUserService#showUser()
	 */
	public List<User> showUser() {
		return userDao.findAll();
	}

	// //////////////////////////////////
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setUserDetailDao(UserDetailDao userDetailDao) {
		this.userDetailDao = userDetailDao;
	}

	public void setUserFriendShipDao(UserFriendShipDao userFriendShipDao) {
		this.userFriendShipDao = userFriendShipDao;
	}

	public void setAdminDao(AdminDao adminDao) {
		this.adminDao = adminDao;
	}

}
